package cheme.ProcessEditor.Parts.Streams.Tag;

import org.eclipse.draw2d.Label;
import org.eclipse.gef.DragTracker;
import org.eclipse.gef.Request;
import org.eclipse.jface.viewers.ICellEditorValidator;

import cheme.ProcessEditor.ValidationEnabledGraphicalViewer;
import cheme.ProcessEditor.DirectEdit.LabelFieldCellEditorvalidator;
import cheme.ProcessEditor.DirectEdit.ValidationMessageHandler;
import cheme.ProcessEditor.DirectEdit.WritableFieldCellEditorValidator;
import cheme.ProcessEditor.Figures.FieldFig;
import cheme.ProcessModel.Interpreter.Parsers.ParserFactory;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.HeatCapacityField;
import cheme.ProcessModel.ProcessUnits.Streams.Tags.LabelField;

public class PartHeatCapacityField extends TagComponentFieldPart implements
		IDragParentOnly {

	@Override
	protected FieldFig createFigure()
	{
		FieldFig fig = super.createFigure();
		fig.setToolTip(new Label(getModel().getToolTipText()));
		
		/*This was coded to check that if Label name already exists than
		 * new stream label msut not be created with that name.
		 * E.g. Rename M1 to M2. Now create another stream. 
		 * It will also have M2. How to avoid this???
		 * LabelField f = getModel(); 
		Interpreter interpreter = ModelEnv.getInstanceFor(f).getInterpreter();
		String value = (String)fig.getText();
		ModelElementData modelData = interpreter.getModelData(value);
		if(interpreter.modelExists(value) && //name is new
				   (null != modelData && f != modelData.getModel()))
				{
					int l = 00;
				}*/
		//fig.setToolTip(new Label("A label naming the stream for reference in equations.\n(Double click or press space-bar to edit)"));
		return fig;
	}
	
	@Override
	public DragTracker getDragTracker(Request request)
	{
		return new ChemeDragEditPartsTracker(this);
	}
	
	@Override
	public HeatCapacityField getModel() {
		// TODO Auto-generated method stub
		return (HeatCapacityField)super.getModel();
	}

	@Override
	protected ICellEditorValidator getCellValidator()
	{
		ValidationEnabledGraphicalViewer viewer = (ValidationEnabledGraphicalViewer)getViewer();
		ValidationMessageHandler handler = viewer.getValidationHandler();
		
		WritableFieldCellEditorValidator validator = new WritableFieldCellEditorValidator(handler, null);
			
		validator.setParser(ParserFactory.getParser(getModel().getType()));
		return validator;
	}
}
